package droid.insane.logic;

import java.util.ArrayList;

import android.util.Log;
import droid.insane.database.DBHandler;

/**
 * @author Sandamal
 *Acts as a mediator between PortfolioWindow and the database
 */
public class PortfolioManager {
	private static PortfolioManager managerInstance;
	private DBHandler dbHandler;

	private PortfolioManager() {
	}

	/**
	 * @return an instance of the manager
	 */
	public static PortfolioManager getManager() {
		if (managerInstance == null) {
			managerInstance = new PortfolioManager();
		}
		return managerInstance;
	}

	/**
	 * @return the names of all the companies
	 */
	public String[] getAllStockNames() {
		String[] stockCodes;
		// open the data base to read values
		dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToRead();
			stockCodes = dbHandler.getAllStockCodes();
			dbHandler.close();
			return stockCodes;
		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
			return new String[] {};
		}

	}

	/**
	 * @param portfolioID
	 * @return portfolio data from the database
	 */
	public ArrayList<String[]> getPorfolioDataFromDB(int portfolioID) {
		// open the data base to read values
		ArrayList<String[]> rowList = new ArrayList<String[]>();
		dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToRead();
			rowList = dbHandler.getPortfolioData(portfolioID);
			dbHandler.close();

		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
		}
		return rowList;
	}

	/**
	 * @param portfolioID
	 * @param stock - company symbol
	 * @param price - price of a share
	 * @param quantity - the amount
	 * @return
	 */
	public boolean addtoPortfolio(int portfolioID, String stock, String price,
			String quantity) {
		int quantityNum = Integer.parseInt(quantity);
		double cost = Float.parseFloat(price) * quantityNum;
		dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToWrite();
			dbHandler.insertToPortfolio(portfolioID, stock, quantityNum, cost);
			//dbHandler.addTransaction(portfolioID, stock, quantityNum, cost);
			dbHandler.close();
			return true;

		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
		}
		return false;

	}

//	public void testAdd() {
//		// TODO Auto-generated method stub
//		dbHandler = DBHandler.getDBHandler(null);
//		try {
//			dbHandler.openToWrite();
//			dbHandler.insertToPortfolio(1, "LGL.N0000", 100, 212.02);
//			dbHandler.close();
//
//		} catch (Exception e) {
//			Log.e(ConstantValues.TAG, e.getMessage());
//
//		}
//	}

	/**
	 * @param portfolioId
	 * @return get data to be displayed in the portfolio window
	 */
	public ArrayList<String[]> getDataForPortfolio(int portfolioId) {
		ArrayList<String[]> rowList;
		dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToWrite();
			rowList = dbHandler.getPortDisplayData(1);
			dbHandler.close();
			return rowList;

		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
			return new ArrayList<String[]>();
		}
	}

	/**
	 * @param portfolioID
	 * @return total value of the portfolio
	 */
	public double[] getTotalValue(int portfolioID) {
		double[] totalValue;
		dbHandler = DBHandler.getDBHandler(null);
		try {
			dbHandler.openToWrite();
			totalValue = dbHandler.getTotalValue(portfolioID);
			dbHandler.close();
			return totalValue;

		} catch (Exception e) {
			Log.e(ConstantValues.TAG, e.getMessage());
			return new double[] { 0, 0 };
		}

	}
}
